<?php

	require('./include/phpsqlajax_dbinfo.php');
	
	function parseToXML($htmlStr){
		$xmlStr=str_replace('&','&amp;',$htmlStr);
		return $xmlStr; 
	}
	
	 // Opens a connection to a MySQL server.
	$connection = mysql_connect ($server, $username, $password);
	if (!$connection) 
	{
	  die('Not connected : ' . mysql_error());
	}
	
	// Sets the active MySQL database.
	$db_selected = mysql_select_db($database, $connection);
	if (!$db_selected) 
	{
	  die ('Can\'t use db : ' . mysql_error());
	}
	
	 //Set result charset to UTF-8
	mysql_query('SET NAMES "utf8"');
	
	/*
	*Get Event Information
	*/
	$getEventInformation='SELECT * FROM `events` WHERE `idEvents`='.$_POST['idEvent'].'';
	$eventInformation = mysql_query($getEventInformation);
	 if (!$eventInformation) 
	 {
	  die('Invalid query: ' . mysql_error());
	 }
	 
	 /*
	 *Get event categories
	 */
	 $getEventCategories = 'SELECT `eventcategory` FROM `eventcategories` , `events_has_eventcategories`
							WHERE `Events_IdEvents` ='.$_POST['idEvent'].'
							AND `idEventCategories` = `EventCategories_idEventCategories`';
	 
	 $eventCategories = mysql_query($getEventCategories);
	 if (!$eventCategories) 
	 {
	  die('Invalid query: ' . mysql_error());
	 }
	 
	 
	 /*
	 *Get keywords
	 */
	 $getKeywords = 	'SELECT `keyword` FROM `keywords`, `keywords_has_events`
						WHERE `Events_IdEvents` ='.$_POST['idEvent'].'
						AND `idKeywords` = `Keywords_idKeywords`';
	 
	 $eventKeywords = mysql_query($getKeywords);
	 if (!$eventKeywords) 
	 {
	  die('Invalid query: ' . mysql_error());
	 }
	 
	 /*
	 *Get links
	 */
	 $getLinks = 	'SELECT `link` FROM `links` WHERE `Events_idEvents`='.$_POST['idEvent'].'';
	 
	 $eventLinks = mysql_query($getLinks);
	 if (!$eventLinks) 
	 {
	  die('Invalid query: ' . mysql_error());
	 }
	 
	 
	 // Creates an array of strings to hold the lines of the XML file.
	header('Content-type: text/xml');
	$xml = array('<?xml version="1.0" encoding="UTF-8"?>');
	$xml[] = '<editableevents>';
	
	// Iterates through the rows, printing a node for each row.
	while ($row = @mysql_fetch_assoc($eventInformation)) 
	{
		$xml[] = '<event>';	
		$xml[] = '<idevent>'.$row['idEvents'].'</idevent>';
		$xml[] = '<Users_idUsers>'.$row['Users_idUsers'].'</Users_idUsers>';
		$xml[] = '<lat>'.$row['lat'].'</lat>';
		$xml[] = '<long>'.$row['long'].'</long>';
		$xml[] = '<name>'.$row['name'].'</name>';
		$xml[] = '<description>'.$row['description'].'</description>';
		$xml[] = '<fee>'.$row['fee'].'</fee>';
			//explode datetime in two pieces
		$dateAndTime=explode(' ',$row['date']);	
		$xml[] = '<date>'.$dateAndTime[0].'</date>';
		$xml[] = '<time>'.$dateAndTime[1].'</time>';	
		$xml[] = '<visibility>'.$row['visibility'].'</visibility>';
		$xml[] = '<town>'.$row['town'].'</town>';
		$xml[] = '<plz>'.$row['plz'].'</plz>';
		$xml[] = '<street>'.$row['street'].'</street>';
		$xml[] = '<nr>'.$row['nr'].'</nr>';
		
		//Get user contacts		
		if($row['Contacts_idContacts']!=null){
			$getContact = 	'SELECT `surname`,`lastname` FROM `contacts` WHERE `idContacts`='.$row['Contacts_idContacts'].'';
		 
			$eventContact = mysql_query($getContact);
			if (!$eventContact) 
			{
				die('Invalid query: ' . mysql_error());
			}
			while ($row = @mysql_fetch_assoc($eventContact)) 
			{
				$xml[] = '<contact>'.$row['lastname'].', '.$row['surname'].'</contact>';
			}
		}else{
			$xml[] = '<contact></contact>';
		}
		
		//Get categories
		$xml[] = '<categories>';
		while ($row = @mysql_fetch_assoc($eventCategories)) 
		{
			$xml[] = $row['eventcategory'].', ';
		}
		$xml[] = '</categories>';
		
		//get keywords
		$xml[] = '<keywords>';
		while ($row = @mysql_fetch_assoc($eventKeywords)) 
		{
			$xml[] = $row['keyword'];
		}
		$xml[] = '</keywords>';
		
		//get links
		$xml[] = '<link>';
		while ($row = @mysql_fetch_assoc($eventLinks)) 
		{
			$xml[] = $row['link'];
		}
		$xml[] = '</link>';
		$xml[] = '</event>';
	} 
	
	// End XML file
	$xml[] = '</editableevents>';
	$xmlOutput = join($xml);
	
	//Output XML file
	echo $xmlOutput;

?>